/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-27
*/
package com.rzico.account.controller.member;

import com.alibaba.fastjson.JSON;
import com.rzico.account.entity.Payment;
import com.rzico.account.model.Attach;
import com.rzico.account.service.PaymentService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Card;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Vip;
import com.rzico.basics.model.CardView;
import com.rzico.basics.service.CardService;
import com.rzico.basics.service.MemberService;
import com.rzico.basics.service.VipService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 付费会员接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-27
 */
@Api(description = "付费会员接口")
@RestController
@RequestMapping("/member/VIP")
public class SuperVipController extends BaseController {

    @Autowired
    private CardService cardService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SysSequenceService sysSequenceService;

    @Autowired
    private VipService vipService;

    @Autowired
    private PaymentService paymentService;


    /**
     * 查询我的VIP及福利
     *
     * @return
    */
    @ApiOperation("查询我的VIP及福利")
    @GetMapping("/getVIP")
    public CommResult<Card> getVIP(Pageable pageable) {

        CommResult commResult = new CommResult();
        Member member = memberService.getCurrent();
        Map<String, Object> data = new HashMap<>();
        if (member!=null) {
            data.put("member",member);
        }

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",member.getEnterpriseId());
        //params.put("isShow",true);
        params.put("sortField","vip");
        params.put("sortType","asc");
        List<Vip> vipList = vipService.selectList(params);

        Vip vip = null;
        for (Vip v:vipList) {
            if (v.getMethod().equals(3)) {
                vip = v;
            }
        }

        if (vip==null) {
            for (Vip v:vipList) {
                if (v.getVip().equals(member.getVip()+1)) {
                    vip = v;
                }
            }
        }

        if (vip==null && vipList.size()>0) {
            vip = vipList.get(vipList.size()-1);
        }

        data.put("vip",vip);

        //会员福利相关信息
        commResult.setData(data);

        return commResult;

    }

    /**
     * 购买会员
     *
     * @return
    */
    @ApiOperation("购买会员")
    @GetMapping("/buyVIP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vipType", value = "会员类型(0.体验会员(1月)，1.VIP会员(1年)，2.永久会员)", dataType = "String", paramType = "query"),
    })
    public CommResult<CardView> buyVIP(Integer vipType) {
        Member member = memberService.getCurrent();

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",member.getEnterpriseId());
        List<Vip> vipList = vipService.selectList(params);

        Vip vip = null;
        for (Vip v:vipList) {
            if (v.getMethod().equals(3)) {
                vip = v;
            }
        }

        if (vip==null) {
            return CommResult.error("没有开通付费会员");
        }

        if (vip.getAmount().compareTo(BigDecimal.ZERO)<=0) {
            return CommResult.error("会员设置不正确");
        }
        if (vip.getQuantity()<=0L) {
            return CommResult.error("会员设置不正确");
        }

        Payment payment = new Payment();
        payment.setCreateDate(new Date());
        payment.setDeleted(false);
        payment.setSn(sysSequenceService.generate("payment"));
        Attach attach = new Attach();
        attach.setId(String.valueOf(vipType));
        attach.setCode(member.getMobile());
        if (vipType.equals(0)) {
            if (member.getVipExpired()!=null) {
                return CommResult.error("只针对新用户");
            }
            payment.setAmount(vip.getAmount().divide(new BigDecimal(12),2,BigDecimal.ROUND_DOWN));
            attach.setDescription("体验会员");
            payment.setMemo("缴费-体验会员");
        } else
        if (vipType.equals(1)) {
            payment.setAmount(vip.getAmount());
            attach.setDescription("VIP年费");
            payment.setMemo("缴费-VIP年费");
        } else
        {
            payment.setAmount(new BigDecimal(vip.getQuantity()));
            attach.setDescription("永久会员");
            payment.setMemo("缴费-永久会员");
        }
        payment.setRefund(BigDecimal.ZERO);
        payment.setFee(BigDecimal.ZERO);
        payment.setEnterpriseId(member.getEnterpriseId());
        payment.setMemberId(member.getId());
        payment.setExpire(DateUtils.addMinutes(new Date(), 30));
        payment.setStatus(Payment.STATUS_WAITING);
        payment.setType(Payment.TYPE_VIP);
        payment.setMethod(Payment.METHOD_ONLINE);
        payment.setWay(1);
        payment.setAttach(JSON.toJSONString(attach));
        paymentService.insertUseGeneratedKeys(payment);

        return CommResult.success(payment);

    }


}
